package com.mg.common.vo;

import lombok.Data;

/**
 * 统一响应结果集
 *
 * @author tomyeung-李尐达
 */
@Data
public class ResponseResult<T> {
    private T data;
    private Integer code;
    private String msg;

    /**
     * 无参构造方法
     * 默认成功
     */
    private ResponseResult() {
        this.code = ResponseStatus.SUCCESS.getCode();
        this.msg = ResponseStatus.SUCCESS.getMsg();
    }

    /**
     * @param data 数据
     * @return 成功结果集
     */
    public static <T> ResponseResult ok(T data) {
        return new ResponseResult(data, ResponseStatus.SUCCESS);
    }

    /**
     * @return 成功结果集
     */
    public static <T> ResponseResult ok() {
        return new ResponseResult(ResponseStatus.SUCCESS);
    }

    /**
     * @return 失败通用提示结果集
     */
    public static ResponseResult error() {
        return new ResponseResult(ResponseStatus.FAIL);
    }
    /**
     * @param status  错误响应状态枚举
     * @return 带有指定 错误响应状态枚举
     */
    public static ResponseResult error(ResponseStatus status) {
        return new ResponseResult(status);
    }
    /**
     * @param data 数据
     * @return 成功结果集
     */
    public static <T> ResponseResult error(T data) {
        return new ResponseResult(data, ResponseStatus.FAIL);
    }

    /**
     * @param status 响应状态枚举
     */
    private ResponseResult(ResponseStatus status) {
        this.code = status.getCode();
        this.msg = status.getMsg();
    }

    /**
     * @param data   响应数据
     * @param status 响应状态枚举
     */
    private ResponseResult(T data, ResponseStatus status) {
        this.data = data;
        this.code = status.getCode();
        this.msg = status.getMsg();
    }
}
